package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetDrawBookCatalogBean;
import com.rjwh.dingdong.client.bean.localbean.DrawBookTypeListData;
import com.rjwh.dingdong.client.bean.localbean.GetDrawBookCatalogData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.DrawBookCatalogFragmentModel;
import com.rjwh.dingdong.client.mvp.presenter.IDrawBookCatalogFragmentPresenter;
import com.rjwh.dingdong.client.mvp.view.DrawBookCatalogFragmentView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * Created by Kay on 2016-6-3.
 */
public class DrawBookCatalogFragmentPresenter extends BasePresenter<DrawBookCatalogFragmentView> implements IDrawBookCatalogFragmentPresenter {

    private DrawBookCatalogFragmentModel model;

    public DrawBookCatalogFragmentPresenter(Context context, DrawBookCatalogFragmentView mMvpView) {
        super(context, mMvpView);
        model = new DrawBookCatalogFragmentModel(this);
    }

    /**
     * 获取绘本目录
     *
     * @param agecode 年龄段编码
     * @param typeid  绘本类型编码
     */
    @Override
    public void doGetDrawBookCatalog(String agecode, String typeid) {
        model.doGetDrawBookCatalog(agecode, typeid);
    }

    /**
     * 请求结果
     *
     * @param businessType
     * @param mAsResult
     * @param isRefresh
     */
    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            switch (businessType) {
                case NetConstant.GETDRAWBOOKCATALOG:
                    getMvpView().setRefreshing(false);
                    ResGetDrawBookCatalogBean getDrawBookCatalogBean = (ResGetDrawBookCatalogBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && getDrawBookCatalogBean != null) {
                        GetDrawBookCatalogData getDrawBookCatalogData = getDrawBookCatalogBean.getData();
                        if (getDrawBookCatalogData != null) {
                            List<DrawBookTypeListData> booktypelist = getDrawBookCatalogData.getBooktypelist();
                            if (booktypelist != null) {
                                getMvpView().loadData(booktypelist);
                            }else {
                                ToastUtil.showToast(getContext(),getContext().getString(R.string.empty_data));
                            }
                        }
                    }else {
                        ToastUtil.showToast(getContext(),getContext().getString(R.string.refresh_error));
                    }
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }
}
